package com.smart.lab.utils;

import android.text.TextUtils;


import com.smart.lab.model.User;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by liuyan on 2019/9/29.
 */

public class UserInfoUtils {

    public static void setUserId(long userId) {
        SPHelper.getPreferences().saveLong("userId", userId);
    }

    public static long getUserId() {
        return SPHelper.getPreferences().getLong("userId");
    }

    public static void setMemberId(long memberId) {
        SPHelper.getPreferences().saveLong("memberId", memberId);
    }

    public static long getMemberId() {
        return SPHelper.getPreferences().getLong("memberId");
    }


    public static void setIfRemeberPassword(boolean rember) {
        SPHelper.getPreferences().saveBoolean("remberPassword", rember);
    }

    //是否记住密码
    public static boolean getIfRemberPassword() {
        return SPHelper.getPreferences().getBoolean("remberPassword", true);
    }

    public static void setUserName(String userName) {
        SPHelper.getPreferences().saveString("username", userName);
    }

    public static String getUserName() {
        return SPHelper.getPreferences().getString("username");
    }

    public static void setPassword(String password) {
        if (getIfRemberPassword())
            SPHelper.getPreferences().saveString("password", password);
    }

    public static String getPassword() {
        return SPHelper.getPreferences().getString("password");
    }

    public static void setNickName(String NickName) {
        if (TextUtils.isEmpty(NickName)){
            return;
        }
        SPHelper.getPreferences().saveString("NickName", NickName);
    }

    public static String getNickName() {
        return SPHelper.getPreferences().getString("NickName");
    }

    public static void setRealName(String RealName) {
        if (TextUtils.isEmpty(RealName)){
            return;
        }
        SPHelper.getPreferences().saveString("RealName", RealName);
    }

    public static String getRealName() {
        return SPHelper.getPreferences().getString("RealName");
    }

    public static void setOrgId(long orgId) {

        SPHelper.getPreferences().saveLong("orgId", orgId);
    }

    public static long getOrgId() {
        return SPHelper.getPreferences().getLong("orgId");
    }


    public static void setEmail(String Email) {
        if (TextUtils.isEmpty(Email)){
            return;
        }
        SPHelper.getPreferences().saveString("Email", Email);
    }

    public static String getEmail() {
        return SPHelper.getPreferences().getString("Email");
    }

    public static void setMobile(String Mobile) {
        if (TextUtils.isEmpty(Mobile)){
            return;
        }
        SPHelper.getPreferences().saveString("Mobile", Mobile);
    }

    public static String getMobile() {
        return SPHelper.getPreferences().getString("Mobile");
    }

    public static void setToken(String token) {
        SPHelper.getPreferences().saveString("token", token);
    }

    public static String getToken() {
        return SPHelper.getPreferences().getString("token");
    }

    public static void setStatus(int status) {
        SPHelper.getPreferences().saveInt("status", status);
    }

    public static int getStatus() {
        return SPHelper.getPreferences().getInt("status");
    }

    public static void setClient(int client) {
        SPHelper.getPreferences().saveInt("client", client);
    }

    public static int getClient() {
        return SPHelper.getPreferences().getInt("client");
    }

    public static void setUserType(int userType) {
        SPHelper.getPreferences().saveInt("userType", userType);
    }

    public static int getUserType() {
        return SPHelper.getPreferences().getInt("userType");
    }

    /**
     * 保存用户的权限列表
     */
    public static void savePermList(List<String> permList) {
        SPHelper.getPreferences().setDataList("permList", permList);
    }

    public static List<String> getPermList() {
        String permsJson = SPHelper.getPreferences().getString("permList");
        if (TextUtils.isEmpty(permsJson)) {
            return new ArrayList<>();
        } else {
            return SPHelper.getPreferences().getDataList(permsJson, String.class);
        }
    }


    public static void setCookies(HashSet<String> cookies) {
        SPHelper.getPreferences().saveHashSet("cookies", cookies);
    }

    public static HashSet<String> getCookies() {
        return SPHelper.getPreferences().getHashSet("cookies");
    }

    public static void savaUserInfo(User user) {
        setUserId(user.userId);
        setUserName(user.username);
        setNickName(user.nickname);
        setRealName(user.realName);

        if (user.memberId!=null) {
            setMemberId(user.memberId);
        }

        if (!TextUtils.isEmpty(user.password)) {
            setPassword(user.password);
        }

        if (user.deptId != null) {
            setOrgId(user.deptId);
        }

        if (user.client!=null){
            setClient(user.client);
        }

        setMobile(user.mobile);
        setEmail(user.email);
        setStatus(user.status);

        if (user.userType!=null) {
            setUserType(user.userType);
        }
    }

    public static User getUserInfo() {
        User user = new User();
        user.userId = getUserId();
        user.username = getUserName();
        user.nickname = getNickName();
        user.password = getPassword();
        user.email = getEmail();
        user.mobile = getMobile();
        user.status = getStatus();
        user.userType = getUserType();
        user.memberId = getMemberId();
        return user;
    }


    public static void saveUserPermList(List<String> permList) {
        if (permList != null)
            savePermList(permList);
    }

    //是否具有菜单权限
    //TODO 发版打开
    public static boolean hasMenuPerm(String permName) {
        List<String> permList = getPermList();
        if (permList == null || permList.size() == 0 || !permList.contains(permName)) {
            return false;
        }

        return true;
    }



    //标记是否有隐私设置
    public static void setPrivacePolicy(boolean privace) {
        SPHelper.getPreferences().saveBoolean("PrivacePolicy", privace);
    }

    public static boolean getPrivacePolicy() {

        return SPHelper.getPreferences().getBoolean("PrivacePolicy", false);
    }



}
